package com.reader.cms.controller.magazine;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.magazine.ReaderMagazineColumn;
import com.reader.cms.entity.sys.SysRole;
import com.reader.cms.mapper.magazine.ReaderMagazineColumnMapper;
import com.reader.cms.service.magazine.IReaderMagazineColumnService;
import com.reader.core.entity.ResultMessageInfo;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gumenghao
 * @description 杂志栏目有关接口
 * @date 2018-10-20
 */
@RestController
@RequestMapping("/api/readermagazinecolumn")
public class ReaderMagazineColumnController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReaderMagazineColumnController.class);

    @Autowired
    public IReaderMagazineColumnService iReaderMagazineColumnService;

    @Autowired
    public ReaderMagazineColumnMapper readerMagazineColumnMapper;


    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param param       其它查询条件，根据情况动态添加
     * @return
     * @function 分页查询数据
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "param", required = false) String param) {
        Page<Map<String, Object>> list = iReaderMagazineColumnService.selectListPage(currentPage, size, "");
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }


    /**
     * @function 查询下拉box所用的数据
     */
    @GetMapping("selectBox")
    public ResultMessageInfo selectListPage() {
        EntityWrapper<ReaderMagazineColumn> columnEntityWrapper = new EntityWrapper<>();
        columnEntityWrapper.eq("del_flag", 0).eq("enable", 1);
        List<ReaderMagazineColumn> list = iReaderMagazineColumnService.selectList(columnEntityWrapper);
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }

    /**
     * @param readerMagazineColumn 实体对象
     * @return
     * @function 添加新的杂志栏目
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody ReaderMagazineColumn readerMagazineColumn) {
        ValidatorUtil.validateEntity(readerMagazineColumn, AddGroup.class);//非空验证
        EntityWrapper<ReaderMagazineColumn> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("name", readerMagazineColumn.getName());
        Integer count = readerMagazineColumnMapper.selectCount(wrapper);
        if (count != 0) {
            return ResultMessageInfo.ofMessage("杂志栏目名称已存在！！！");
        }
        Integer userId = this.getUserId();
        readerMagazineColumn.setInsertDt(new Date());
        readerMagazineColumn.setDelFlag(0);
        readerMagazineColumn.setInsertUserId(userId);
        readerMagazineColumn.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineColumnService.insertOrUpdate(readerMagazineColumn);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("添加失败！！！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * @param readerMagazineColumn 实体对象
     * @return
     * @function 更新杂志栏目数据
     */
    @PostMapping("update")
    @Transactional
    @ApiOperation(value = "跟据ID列新记录", notes = "id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo update(@RequestBody ReaderMagazineColumn readerMagazineColumn) {
        ValidatorUtil.validateEntity(readerMagazineColumn, AddGroup.class);//非空验证
        EntityWrapper<ReaderMagazineColumn> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("name",
                readerMagazineColumn.getName()).ne("id", readerMagazineColumn.getId());
        Integer count = readerMagazineColumnMapper.selectCount(wrapper);
        if (count != 0) {
            return ResultMessageInfo.ofMessage("杂志栏目名称已存在！！！");
        }
        Integer userId = this.getUserId();
        readerMagazineColumn.setUpdtDt(new Date());
        readerMagazineColumn.setUpdtUserId(userId);
        readerMagazineColumn.setDelFlag(0);
        boolean resultValue = iReaderMagazineColumnService.insertOrUpdate(readerMagazineColumn);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("修改失败！");

        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @param readerMagazineColumn 删除的数据对象
     * @return
     * @function 删除杂志栏目
     */
    @PostMapping("delete")
    @Transactional
    @ApiOperation(value = "跟据ID删除记录", notes = "键为id值为整型数")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo deleteById(@RequestBody ReaderMagazineColumn readerMagazineColumn) {
        Integer userId = this.getUserId();
        readerMagazineColumn.setDelFlag(1);
        readerMagazineColumn.setUpdtDt(new Date());
        readerMagazineColumn.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineColumnService.updateById(readerMagazineColumn);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");

    }

    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除杂志栏目
     */
    @PostMapping("deleteIds")
    @Transactional
    public ResultMessageInfo deleteBatchIds(@RequestBody List<ReaderMagazineColumn> readerMagazineColumns) {
        Integer userId = this.getUserId();
        readerMagazineColumns.forEach(readerMagazineColumn -> {
            readerMagazineColumn.setDelFlag(1);
            readerMagazineColumn.setUpdtDt(new Date());
            readerMagazineColumn.setUpdtUserId(userId);
        });
        boolean resultValue = iReaderMagazineColumnService.updateBatchById(readerMagazineColumns);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }
}